package com.peng.leetcode;

import com.google.common.base.Joiner;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * Main1
 *
 * @author: lupeng6
 * @create: 2023/3/23 16:24
 */
public class Main1 {

    public static void main(String[] args) {
//         ["语文	数学	英语	政治", "132	134	110	87"]
// ["数学	英语	化学	物理", "   134   	  111	 87	  98  "]
        List<String> l1 = Arrays.asList("化学	数学	英语	政治", "132	134	110	87");
        List<String> l2 = Arrays.asList("数学	英语	化学	物理", "   134   	  111	 87	  98  ");
        List<String> strings = new Main1().merge(l1, l2);
        for (String string : strings) {
            System.out.println(string);
        }
    }

    public List<String> merge(List<String> list1, List<String> list2) {
        List<Map<String, String>> like = parse(list1);
        List<Map<String, String>> wenke = parse(list2);
        Set<String> allKemu = new HashSet<>(like.get(0).keySet());
        allKemu.addAll(wenke.get(0).keySet());
        List<String> allKemuList = new ArrayList<>(allKemu);

        List<String> ans = new ArrayList<>();
        ans.add(Joiner.on("\t").join(allKemuList));

        like.addAll(wenke);

        for (Map<String, String> map : like) {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < allKemuList.size(); i++) {
                String kemu = allKemuList.get(i);
                if (map.containsKey(kemu)) {
                    builder.append(map.get(kemu));
                } else {
                    builder.append("0");
                }
                if (i < allKemuList.size() - 1) {
                    builder.append("\t");
                }
            }
            ans.add(builder.toString());
        }
        return ans;
    }


    private List<Map<String, String>> parse(List<String> list) {
        List<Map<String, String>> ans = new ArrayList<>();
        String kemu1 = list.get(0);
        String[] kemuArr = kemu1.split("\t");
        List<String> kemuList = new ArrayList<>();
        for (String s : kemuArr) {
            s = s.trim();
            kemuList.add(s);
        }
        for (int i = 1; i < list.size(); i++) {
            String[] split = list.get(i).split("\t");
            Map<String, String> kemu2scoreMap = new HashMap<>();
            for (int j = 0; j < split.length; j++) {
                String trim = split[j].trim();
                if (StringUtils.isNotBlank(trim)) {
                    kemu2scoreMap.put(kemuList.get(j), trim);
                }
            }
            ans.add(kemu2scoreMap);
        }
        return ans;
    }
}
